/*
 * Serum
 *
 * @license      GNU Lesser General Public Licence see LICENCE-LGPL file or http://www.gnu.org/licenses/lgpl.html
 */

/**
 * Form panel
 */
Serum.Panel.Module.Form = Ext.extend(Ext.Panel, {

    /**
     * Constructor
     *
     * @param   {Object}    config  Custom configuration
     * @param   {Object}    viewer
     */
    constructor: function(config, viewer) {
        this.viewer = viewer;
        Ext.apply(this, config);

        this.form       = null;

        Serum.Panel.Module.Form.superclass.constructor.call(this, {
            title:          __('base~form_title'),

            layout:         'fit',

            margins:        '0 5 5 0',
            cmargins:       '0 5 5 5',

            split:          true,
            collapseFirst:  false,
            collapsible:    true,
            collapsed:      true,
            animCollapse:   false,

            items:          []
        });

        Serum.App.get().eventListener.on('actionchanged',   this.onActionChanged,   this);
        Serum.App.get().eventListener.on('languagechanged', this.onLanguageChanged,   this);
        Serum.App.get().eventListener.on('hideform',        this.onHideForm,   this);

        this.on('beforecollapse',   this.onBeforeCollapse,  this);
        this.on('beforeexpand',     this.onBeforeExpand,    this);
    },

    /**
     * Handler for 'actionchanged' event
     *
     * @param   {Object}    params  Event parameters
     */
    onActionChanged: function(params) {
        var formClass = Serum.Selector.get().currentModule().formClass();

        if (formClass !== null) {
            this.form = eval('new ' + formClass + '()');
        }

        if (formClass !== null && this.form.isFormAction()) {
            this.form.on('formbuilded', this.onFormBuilded, this);
            this.form.preload();
            this.form.load();

        } else if (Serum.Preferences.get().getValue('LIST_AFTER_ACTION') == 1) {
            this.collapse(false);
        }
    },

    /**
     * Handler for 'languagechanged' event
     *
     * @param   {Object}    params Ev
     */
    onLanguageChanged: function(params) {
        this.onActionChanged(params);
    },

    /**
     * Handler for 'hideform' event
     *
     * @param   {Object}    params  Event parameters
     */
    onHideForm: function(params) {
        this.collapse(false);
    },

    /**
     * Handler for 'formbuilded' event
     *
     * @param   {Object}    params  Event parameters
     */
    onFormBuilded: function(params) {
        if (typeof this.items !== 'undefined' && this.items.getCount() > 0) {
            var formerForm = this.items.first();
            formerForm.un('formbuilded', this.onFormBuilded, this);

            this.remove(formerForm);
            delete formerForm;
        }

        this.add(this.form);

        var title = this.form.getTitle();
        this.setTitle(title);

        this.expand(false);
        this.doLayout();

        this.form.cleanErrors();
    },

    /**
     * Handler for 'beforecollapse' event
     *
     * @param   {Object}    params  Event parameters
     */
    onBeforeCollapse: function(params) {

    },

    /**
     * Handler for 'beforeexpand' event
     *
     * @param   {Object}    params  Event parameters
     */
    onBeforeExpand: function(params) {

    }
});